package com.lhl.blog.common.interceptor;

import com.lhl.blog.common.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //校验token的合法性
        //获取token
        String token = request.getHeader("user_header_token");
        log.info("从header中获取到token，token:{}",token);
        //校验token是否存在
        Claims claims = JwtUtils.parseJwt(token);
        //token不合法
        if (claims == null){
            //将状态码设为401
            response.setStatus(401);
            return false;
        }
        return true;

    }
}
